//
//  ViewController.m
//  UI26_AVPlayer
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
// Audio/Video
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *playerView;/**< 播放视图 */
@property (nonatomic, strong)AVPlayer *player;/**< 播放器对象 */
@property (nonatomic, strong)AVPlayerLayer *playerLayer;/**<* 播放图层 */
@property (nonatomic, strong)AVPlayerItem *playerItem;/**< 播放对象 */
@property (nonatomic, assign)BOOL isPlay;/**< 是否播放 */


@property (weak, nonatomic) IBOutlet UIView *bottomView;/**< 底部控制区 */
@property (weak, nonatomic) IBOutlet UISlider *progress;/**< 进度条 */
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;/**< 时间显示 */

@property (nonatomic, assign)BOOL isDismiss;/**< 是否隐藏 */
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 初始化播放器
    [self createAVPlayer];
    // 播放
    [_player play];
    // 开始播放
    _isPlay = YES;
    // 初始化时间显示
    _timeLabel.text = @"00:00/00:00";
    // 添加手势 隐藏边栏
    [self addTapToDismissBar];
    
    
    
}
#pragma mark - 添加手势
- (void)addTapToDismissBar
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_playerView addGestureRecognizer:tap];
}
#pragma mark - 隐藏边栏
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 初始播放 还未隐藏 isDissmiss = NO;
    // 隐藏
    if (!_isDismiss) {
        // 隐藏边栏
        [UIView animateWithDuration:0.5 animations:^{
            _bottomView.alpha = 0;
        }];
    }else{
        // 显示
        [UIView animateWithDuration:0.5 animations:^{
            _bottomView.alpha = 0.8;
        }];
    }
    _isDismiss = !_isDismiss;
}
#pragma mark - 初始化播放器
- (void)createAVPlayer
{
    // 视频网址
    NSString *str = @"http://flv2.bn.netease.com/videolib3/1505/24/HYUCE6348/SD/HYUCE6348-mobile.mp4";
    NSURL *url = [NSURL URLWithString:str];// 统一资源定位符
    // 创建播放对象(playeritem)
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    // 创建播放器(player)
    self.player = [AVPlayer playerWithPlayerItem:_playerItem];
    // 创建显示layer(playerLayer)
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    // layer需要设置大小
//    _playerLayer.frame = CGRectMake(5, 5, self.view.bounds.size.width - 10, self.view.bounds.size.height - 10);
    _playerLayer.frame = CGRectMake(5, 5, 365, 235);
    // 视频填充模式
    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    // 将显示的图层 放在页面里显示
    [_playerView.layer insertSublayer:_playerLayer atIndex:0];
    
  
    
    
}
#pragma mark - 播放/暂停
- (IBAction)play:(UIButton *)sender {
    // BOOL判断
    if (_isPlay) {
        // 正在播放时 暂停
        [_player pause];
        [sender setTitle:@"播放" forState:UIControlStateNormal];
    }else{
        [_player play];
//        sender.titleLabel.text = @"暂停";
        [sender setTitle:@"暂停" forState:UIControlStateNormal];
    }
    _isPlay = !_isPlay;
}
- (IBAction)progressAction:(UISlider *)sender {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
